package dao;

import model.Folk;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class FolkDateBase {
    private String dbdriver="com.mysql.jdbc.Driver";
    private String dburl="jdbc:mysql://localhost:3306/sctu-javaweb-2018?serverTimezone=UTC&characterEncoding=UTF-8";
    private String dbuser="root";
    private String dbpassword="123456";

    public List<Folk> getfolkList()throws Exception{
        List<Folk> folkList=new ArrayList<Folk>();
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;

        Class.forName(dbdriver);
        connection= DriverManager.getConnection(dburl,dbuser,dbpassword);

        String sql="select * from folk limit 2";

        preparedStatement=connection.prepareStatement(sql);
        resultSet=preparedStatement.executeQuery();
        while (resultSet.next()){
            Folk folk=new Folk();
            folk.setId(resultSet.getInt(1));
            folk.setUsername(resultSet.getString(2));
            folk.setFolkName(resultSet.getString(3));
            folk.setFolkImage(resultSet.getString(4));
            folk.setFolkIntroduce(resultSet.getString(5));
            folk.setFolkPrice(resultSet.getInt(6));
            folk.setFolkType(resultSet.getString(7));
            folk.setFolkBed(resultSet.getInt(8));
            folk.setFolkPeople(resultSet.getInt(9));
            folk.setFolkArea(resultSet.getString(10));
            folkList.add(folk);
        }
        connection.close();
        preparedStatement.close();
        resultSet.close();
        return folkList;
    }
}
